<template>
  <div>
    <subscate :data="cate"/>
    <van-field label="分类名称" v-model="name"></van-field>
    <van-field label="分类参数" v-model="params"></van-field>
    <van-button round @click="add">添加</van-button>
  </div>
</template>

<script>
import subscate from "./subscate";

export default {
  name: "add_cate",
  data() {
    return {
      cate: {},
      pid: 0,
      name: "",
      params: ""
    }
  },
  components: {
    'subscate': subscate
  },
  methods: {
    get_cate() {
      this.$axios.get('/courses/cates')
        .then(res => {
          if (res.data.code == 200) {
            this.cate = {'id': 0, 'name': '333', 'son': res.data.list}
          }
        })
    },
    add() {
      if (this.params) {
        this.params = JSON.stringify(this.params)
      }
      if (localStorage.getItem('pid')) {
        this.pid = localStorage.getItem('pid')
      }
      let data = {'name': this.name, 'pid': this.pid, 'params': this.params}
      this.$axios.post('/courses/cates', data)
        .then(res => {
          if (res.data.code == 200) {
            alert('添加成功')
            this.name = ""
            this.params = ""
            this.get_cate()
          }
        })
    }
  },
  mounted() {
    this.get_cate()
  }
}
</script>

<style scoped>

</style>
